package vp.remedies.dr.home.fragment;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import vp.remedies.dr.home.R;
import vp.remedies.dr.home.activity.MainRemediesActivity;
import vp.remedies.dr.home.adapter.HomeContentAdapter;
import vp.remedies.dr.home.listviewfeed.data.FeedItem;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

public class HomeFragmentNew extends ListFragment {

	private static final String HOME_PAGE_CONTENT_TXT_EN = "home_page_content_en.txt";
	public static final String KEY_IMAGE = "image";
	public static final String KEY_STATUS = "status";
	public static final String KEY_PROFILE_PIC = "profilePic";
	public static final String KEY_TIME_STAMP = "timeStamp";
	public static final String KEY_XML_FILE_NAME = "xmlFileName";
	public List<FeedItem> feedItems;
	public static final String KEY_DESC = "desc";
	public static final String KEY_SLOGAN = "slogan";
	public static final String KEY_ID = "id";
	
	ListView list;
	HomeContentAdapter adapter;

	public String getAssetXMLFile(String xmlFileName) throws IOException {
		AssetManager manager = getActivity().getAssets();
		InputStream file = manager.open(xmlFileName);
		byte[] xmlContentBytes = new byte[file.available()];
		file.read(xmlContentBytes);
		file.close();

		String xmlString = new String(xmlContentBytes);
		return xmlString;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);


		String data1 = null;
		try {
			data1 = getAssetJSONFile();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		JSONObject response = null;
		try {
			response = new JSONObject(data1);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		feedItems = new ArrayList<FeedItem>();
		parseJsonFeed(response);

		// Getting adapter by passing xml data ArrayList
		adapter = new HomeContentAdapter(getActivity(), feedItems);
		setListAdapter(adapter);
		/*
		 * listAdapter = new FeedListAdapter(getActivity(), feedItems);
		 * setListAdapter(listAdapter);
		 */
	}

	public String getAssetJSONFile() throws IOException {
		AssetManager manager = getActivity().getAssets();
		InputStream file = manager.open(HOME_PAGE_CONTENT_TXT_EN);
		byte[] homePageContentBytes = new byte[file.available()];
		file.read(homePageContentBytes);
		file.close();

		String homePageContentJSONString = new String(homePageContentBytes);
		return homePageContentJSONString;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_home, container, false);
		// return super.onCreateView(inflater, container, savedInstanceState);
	}

	@Override
	public void onListItemClick(ListView list, View v, int position, long id) {

		Toast.makeText(getActivity(),
				getListView().getItemAtPosition(position).toString(),
				Toast.LENGTH_LONG).show();

		// On selecting single track get song information

		FeedItem feedItem = feedItems.get(position);
		// Toast.makeText(activity, htmlPageName, Toast.LENGTH_LONG).show();

		String xmlFileName = feedItem.getXmlFileName();
		Intent i = null;

		if (xmlFileName != null) {
			try {
				if (xmlFileName.contains(".xml")) {
					i = new Intent(getActivity(), MainRemediesActivity.class);
					i.putExtra(MainRemediesActivity.KEY_XML_FILE_NAME,
							xmlFileName);
				} else {
					i = new Intent(getActivity(), Class.forName(xmlFileName));
				}
				startActivity(i);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}

	}

	/**
	 * Parsing json reponse and passing the data to feed view list adapter
	 * */
	private void parseJsonFeed(JSONObject response) {
		try {
			JSONArray feedArray = response.getJSONArray("feed");

			for (int i = 0; i < feedArray.length(); i++) {
				JSONObject feedObj = (JSONObject) feedArray.get(i);

				FeedItem item = new FeedItem();
				item.setId(feedObj.getInt("id"));
				item.setName(feedObj.getString("name"));

				// Image might be null sometimes
				String image = feedObj.isNull(KEY_IMAGE) ? null : feedObj
						.getString(KEY_IMAGE);
				item.setImge(image);
				item.setStatus(feedObj.getString(KEY_STATUS));
				item.setProfilePic(feedObj.getString(KEY_PROFILE_PIC));
				item.setTimeStamp(feedObj.getString(KEY_TIME_STAMP));
				item.setXmlFileName(feedObj.getString(KEY_XML_FILE_NAME));
				item.setDesc(feedObj.getString(KEY_DESC));
				item.setSlogan(feedObj.getString(KEY_SLOGAN));
				// url might be null sometimes
				String feedUrl = feedObj.isNull("url") ? null : feedObj
						.getString("url");
				item.setUrl(feedUrl);

				feedItems.add(item);
			}

			// notify data changes to list adapater
			// listAdapter.notifyDataSetChanged();
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

}
